/*
 * ****************************************************************************
 * Copyright: 2020-2030, YZChain Tech. Co., Ltd.
 * File name:
 * Description:
 * Author: Dc luo
 * Version: v1.0
 * Date: $DATE
 * History:
 * ***************************************************************************
 */

package com.srp.blockchain.bcos.controller;

import com.srp.blockchain.bcos.dto.BlockInfoDTO;
import com.srp.blockchain.bcos.dto.ChainStatusDTO;
import com.srp.blockchain.bcos.dto.TransactionDTO;
import com.srp.blockchain.bcos.service.BcosService;
import com.srp.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;


@Slf4j
@Api(tags = "区块链状态")
@RestController
@ApiSort(value = 0)
@RequestMapping("/chain")
public class ChainController {

    @Autowired
    private BcosService chainService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "链状态查询", notes = "json object组织的数据返回格式")
    @GetMapping("/info")
    public Result queryChainInfo() {
        ChainStatusDTO result = chainService.getChainStates();
        if (result == null) {
            return Result.error("状态获取失败");
        }
        return Result.OK(result);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "交易信息和回执查询", notes = "包含交易数据和交易回执，input&output需要解码")
    @GetMapping("/transaction/{hash}")
    public Result getTransactionInfo(@PathVariable String hash) {
        TransactionDTO result = chainService.queryTransByHash(hash);
        if (result == null) {
            return Result.error("交易信息查询失败");
        }
        return Result.OK(result);
    }


    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "区块信息查询", notes = "根据区块哈希查询对应信息，包括区块包含的交易详情")
    @GetMapping("/block/{hash}")
    public Result getBlockInfo(@PathVariable String hash) {
        BlockInfoDTO result = chainService.queryBlockByHash(hash);
        if (Objects.isNull(result)) {
            return Result.error("区块信息查询失败");
        }
        return Result.OK(result);
    }

}
